﻿package com.shangda.util;


import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Properties;

public class JdbcUtil {
	
	public static Connection getConnection(){
		try {
			FileInputStream in = new FileInputStream("properties/config.properties");
			Properties p = new Properties();
	    	p.load(in);
	    	String driver = "com.microsoft.sqlserver.jdbc.SQLServerDriver";
			String user = p.getProperty("user");
			String password = p.getProperty("password");
			String url = p.getProperty("url");
			in.close();
			Class.forName(driver);
			return (Connection) DriverManager.getConnection(url, user, password);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}
	
	public static void release(ResultSet resultSet,Statement statement,Connection con){
		if (resultSet != null){
			try {
				resultSet.close();
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
		
		if (statement != null){
			try {
				statement.close();
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
		
		if (con != null){
			try {
				con.close();
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
	}
	
	
	public static void releaseNew(ResultSet rs,Connection con,Statement ... statements){
		if (rs != null){
			try {
				rs.close();
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
		
		if (statements != null){
			for (Statement s : statements){
				try {
					s.close();
				} catch (SQLException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
		}
		
		
		if (con != null){
			try {
				con.close();
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
		
		
		
		
	}
	
	
	
	
	
}
